package com.medmarkmobile.connection;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import com.medmarkmobile.gui.Login;
import com.medmarkmobile.gui.MainActivity;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.sax.StartElementListener;

public class ConexaoLogin extends AsyncTask<String, Boolean, Boolean>{
	private ProgressDialog progressDialog;
	private Login myActivity;
	String[] dados = new String[16];
	
	public ConexaoLogin(Login myActivity){
		progressDialog = new ProgressDialog(myActivity);
		this.myActivity = myActivity;
	}
	
	@Override
	protected Boolean doInBackground(String... params) {
		String status = "";
		Connection conn = null;
		Boolean response = false;
		
		try{
			Class.forName("com.mysql.jdbc.Driver").newInstance();
			String url = "jdbc:mysql://54.94.138.7:3306/medmark";
			conn = DriverManager.getConnection(url, "adminmedmark", "adminmedmark123");
			status = "OK";
			
			String query = "SELECT * FROM tb_pacientes WHERE login = '" + params[0] + "' AND senha = '" + params[1] + "'";
			 
		      // create the java statement
			Statement st = conn.createStatement();
		       
		      // execute the query, and get a java resultset
			
			ResultSet rs = st.executeQuery(query);
			rs.next();
			
			if(rs != null){
				String login = rs.getString("login");
				String senha = rs.getString("senha");
				
				dados[0] = rs.getString("nome");
				dados[1] = "" + rs.getBigDecimal("idade");
				dados[2] = "" + rs.getBigDecimal("cpf");
				dados[3] = rs.getString("rua");
				dados[4] = rs.getString("bairro");
				dados[5] = rs.getString("cidade");
				dados[6] = rs.getString("estado");
				dados[7] = rs.getString("complemento");
				dados[8] = "" + rs.getBigDecimal("numero");
				dados[9] = "" + rs.getBigDecimal("telefone");
				dados[10] = rs.getString("sexo");
				dados[11] = rs.getString("estado_civil");
				dados[12] = rs.getString("profissao");
				dados[13] = rs.getString("senha");
				dados[14] = rs.getString("login");
				dados[15] = "" + rs.getBigDecimal("codigo");
				
				if(login.equalsIgnoreCase(params[0]) && senha.equals(params[1])){
					response = true;
				}
			}
		      st.close();
			
		}catch(SQLException e){
			e.printStackTrace();
			status = e.getMessage();
		} catch (InstantiationException e) {
			e.printStackTrace();
			status = e.getMessage();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
			status = e.getMessage();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
			status = e.getMessage();
		}
		return response;
	}
	
	@Override
    protected void onPreExecute() {
        super.onPreExecute();
        progressDialog.setCancelable(true);
        progressDialog.setMessage("Loading...");
        progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        progressDialog.setProgress(0);
        progressDialog.show();
    }
	
	@Override
	protected void onPostExecute(Boolean result){
		progressDialog.dismiss();
		if(result){
			Intent mainIntent = new Intent(myActivity, MainActivity.class).putExtra("dados", dados);
			myActivity.startActivity(mainIntent);
			//myActivity.setLoginResult(true);
		}
		//myActivity.prinLoginResult();
		
	}


}
